//
//  Queue.swift
//  DataStructures
//
//  Created by  iBrother 李彦兵 on 2018/6/16.
//  Copyright © 2018 CEC-CESEC. All rights reserved.
//

import Foundation


public protocol Queue {
    associatedtype Element
    
    /// Insert an elment at the back of the queue
    ///
    /// - Parameter element: the element that will be inserted
    /// - Returns: true if the operation was successful
    mutating func enqueue(_ element: Element) -> Bool
    
    
    /// Remove the elment at the frant of the queue and reture it
    ///
    /// - Returns:
    mutating func dequeue() -> Element?
    
    /// Check if the queue if empty
    var isEmpty: Bool{ get }
    /// Return the elment at the front of the queue without removing it
    var peek: Element? { get }
}



public struct QueueArray<T>: Queue {
    
//    public typealias Element =
    
    private var array: [T] = []
    public init() {}
    
    public var isEmpty: Bool {
        return array.isEmpty
    }
    
    public var peek: T? {
        return array.first
    }
    
    public mutating func enqueue(_ element: T) -> Bool {
        array.append(element)
        return true
    }
    
    public mutating func dequeue() -> T? {
        return isEmpty ? nil: array.remove(at: 0)
    }
}


extension QueueArray: CustomStringConvertible {
    public var description: String {
        return array.description
    }
}
